#!/bin/sh
#
# @file build_features
# @brief rtmtools feature build script
# @author Noriaki Ando <n-ando@aist.go.jp>
#
# * How to build rtmtools
#
# ** Required environment
#
# The following development environment and tools are required to
# build rtmtools.
#
# - jdk
# - ant
# - Eclipse SDK (3.4 or later is required.)
#
# ** Before build tools
#
# Please edit "version" text to set version number of the tools. This
# is a kind of bash script.
#
# Example:
# -------
# VERSION=1.1.0
# PROJECT_VERSION=${VERSION}.rc7v$(date +%Y%m%d)
#
# ** Environment variables
#
# To build rtmtools, some environmental variables can be set. In most
# case, these variables are automatically set.
#
# - ECLIPSE_HOME: A directory path to an Eclipse SDK. Under this
#                 directory, .eclipseproduct, eclipse.ini, plugins and
#                 eclipse executable should exist. If this
#                 env.variable is not set, this script tries to search
#                 eclipse directory under some directories that is set
#                 in a env.variable ECLIPSE_DIRS written in the head
#                 of this script.
#
# - JAVA_HOME: A directory JDK installed. If this variable is not set,
#                 this script tries to estimate JDK directory to
#                 resolve symbolic link of javac executable.
#
# - VERSION: A simple version number for the tools like 1.1.1. This is
#                 used for actual bundles' version number with
#                 PROJECT_VERSION number.
#
# - PROJECT_VERSION: Project version number is full version string
#                 with simple version and suffix like
#                 rc120121212. This version string is used actual jar
#                 file name.
#
# - JARDIR: A directory to store jar files. Default directory is "jar".
#                 This directory is  temporary jar files store place.
#
# - DISTDIR: A directory to be archived for distribution package.
#                 This directory name becomes archive package name.
#                 Default name is openrtm-x.y.z. x, y, z are version
#                 number which is defined in version text file.
#
# - PLUGINS_DIR: A directory rtmtools plugins stored. Plugin versions
#                 should be same as versions which obtained from
#                 version text file. Usually this variable can be set
#                 automatically as $JARDIR.
#
# - ANT_DiR: Eclipse ant plugin directory. This variable would be set
#                 automatically as
#                 ECLIPSE_HOME/org.apache.ant.<something>.
#

# Eclipse search directories
ECLIPSE_DIRS="$HOME/eclipse $HOME ../ ../../ ../..//usr/lib/ /usr/share"

# Ant eclipse plugin location
ANT_HOME_DEFAULT="${ECLIPSE_HOME}/plugins/org.apache.ant_1.7.0.v200803061910/"

# Default
JARDIR_DEFAULT="jar"

# target projects
PROJECTS="jp.go.aist.rtm.toolscommon.profiles
    jp.go.aist.rtm.toolscommon.profiles.nl1
    jp.go.aist.rtm.toolscommon
    jp.go.aist.rtm.toolscommon.nl1
    jp.go.aist.rtm.rtcbuilder
    jp.go.aist.rtm.rtcbuilder.nl1
    jp.go.aist.rtm.rtcbuilder.java
    jp.go.aist.rtm.rtcbuilder.python
    jp.go.aist.rtm.rtcbuilder.lua
    jp.go.aist.rtm.repositoryView
    jp.go.aist.rtm.repositoryView.nl1
    jp.go.aist.rtm.nameserviceview
    jp.go.aist.rtm.nameserviceview.nl1
    jp.go.aist.rtm.systemeditor
    jp.go.aist.rtm.systemeditor.nl1"

#============================================================
# functions
#============================================================
#------------------------------------------------------------
# getopt
#
# This function gets command line options
# ------------------------------------------------------------
getopt()
{
    if test $# = 1 ; then
        arg=$1
        if test "x$arg" = "xclean" ; then
            TARGET=$1
            return 0
        fi
        if test "x$arg" = "xrevert" ; then
            echo "Reverting updated site.xml"
            rm sitetool/openrtp_site/site.xml
            svn update sitetool/openrtp_site/site.xml
            exit 0
        fi
    fi
}

#------------------------------------------------------------
# get_version
#
# This function gets version number and project version string
# from version text file.
# ------------------------------------------------------------
get_version()
{
    if test "x$VERSION" = "x" || test "x$PROJECT_VERSION" = "x" ; then
        echo "Environment variable VERSION/PROJECT_VERSION is not set."
        echo "Getting from ./version text."
        if test ! -f version.txt ; then
            echo "version.txt not found. Plugins should be built at first."
            echo "Aborting."
            exit 1
        fi
        . ./version.txt
    fi
    export VERSION
    export PROJECT_VERSION

    if test "x$DISTDIR" = "x" ; then
        DISTDIR=openrtp-$VERSION
    fi
    if test "x$JARDIR" = "x" ; then
        JARDIR=$JARDIR_DEFAULT
    fi
}

#------------------------------------------------------------
# find_eclipsehome
#
# This function checks ECLIPSE_HOME env variable and if it is
# not set, it searches an eclipse directory under ECLIPSE_DIRS,
# and set ECLIPSE_HOME env variable.
# ------------------------------------------------------------
find_eclipsehome()
{
    if test ! "x$ECLIPSE_HOME" = "x" ; then
        if test -d $ECLIPSE_HOME ; then
            return 0
        fi
        echo "ECLIPSE_HOME $ECLIPSE_HOME does not exist."
    fi
    echo "Environment variable ECLIPSE_HOME is not set. Searching..."
    for d in $ECLIPSE_DIRS ; do
        if test ! -d $d ; then
            continue
        fi
        tmp=`find -L $d -name .eclipseproduct`
        if test "x$tmp" = "x" ; then
            continue
        fi
        for e in $tmp ; do
            edir=`dirname $e`
            if test -f $edir/eclipse.ini && test -d $edir/plugins ; then
                export ECLIPSE_HOME="$edir"
                return 0
            fi
        done
    done
    echo "eclipse not found. Please install eclipse and set ECLIPSE_HOME."
    exit 1
}

#------------------------------------------------------------
# find_javahome
#
# This function estimate JAVA_HOME from javac, which usually
# is a symbolic link to $JAVA_HOME/bin/javac.
#------------------------------------------------------------
find_javahome()
{
    if test ! "x$JAVA_HOME" = "x" ; then
        if test -d $JAVA_HOME && test -f $JAVA_HOME/bin/javac ; then
            return 0
        fi
        echo "JDK cannot be found under JAVA_HOME: $JAVA_HOME"
    fi
    echo "Valid Environment variable JAVA_HOME is not set. Searching..."
    tmp=`readlink -e $(which javac)`
    jdk_path=`dirname $tmp | sed 's/\/bin$//'`
    if test "x$jdk_path" = "x" ; then
        echo "JDK not found. Please install JDK and set JAVA_HOME."
        exit 1
    fi
    export JAVA_HOME=$jdk_path
    return 0
}

#------------------------------------------------------------
# find_antplugin
#
# This function find Eclipse's and plugin under ECLIPSE_HOME.
#------------------------------------------------------------
find_antplugin()
{
    if test ! "x$ANT_HOME" = "x" ; then
        echo "ANT_HOME: $ANT_HOME is set."
        if test -d $ANT_HOME ; then
            echo "ANT_HOME: $ANT_HOME exists. Contents are the following."
            ls $ANT_HOME
            return 0
        fi
        echo "ANT_HOME directory $ANT_HOME not found. Searching other Ant..."
    else
        echo "Environment variable ANT_HOME does not exist. Searching..."
    fi

    other_ant=`find $ECLIPSE_HOME/plugins -maxdepth 1 -type d -name 'org.apache.ant*'`
    echo "Found: $other_ant"
    if test "x$other_ant" = "x" ; then
        echo "Ant plugin was not found."
        echo "Please install eclipse with ant plugin such as org.apache.ant.*"
        echo "Aborting."
        return 1
    fi
    for a in $other_ant ; do
        ANT_HOME=$other_ant
        echo "ANT_HOME: $ANT_HOME is set."
        return 0
    done
}

#------------------------------------------------------------
# check_plugins
#------------------------------------------------------------
check_plugins()
{
    if test ! "x$PLUGINS_DIR" = "x" ; then
        JARDIR=$PLUGINS_DIR
    fi
    echo "Checking plugin jar files..."
    for p in $PROJECTS ; do
        tmp="${p}_${PROJECT_VERSION}.jar"
        if test ! -f `pwd`/$JARDIR/$tmp ; then
            echo "[ERROR] $tmp not found"
            echo "Please build plugins at first."
            exit 1
        fi
        echo "[OK]    $tmp found"
    done
    PLUGINS_DIR=`pwd`/$JARDIR
    echo "PLUGINS_DIR: $JARDIR is set."
    return 0
}

#------------------------------------------------------------
# build_features
#
# Building features:
# - copying plugins
# - signing plugins
# - building features
# - copying features
#------------------------------------------------------------
build_features()
{
    echo "Getting plugins (get.plugins)"
    ant -lib lib get.plugins
    if test $? -ne 0 ; then
        echo "Error in get.plugins target. Aborting..."
        exit 1
    fi
    
    echo "Signing features (feature.sign.gen)"
    ant -lib lib feature.sign.gen
    if test $? -ne 0 ; then
        echo "Error in feature.sign.gen target. Aborting..."
        exit 1
    fi
    
    echo "Building features (build.features)"
    ant -lib lib build.features
    if test $? -ne 0 ; then
        echo "Error in build.features target. Aborting..."
        exit 1
    fi
    
    echo "Deploying features to site (deploy.site)"
    ant -lib lib deploy.site
    if test $? -ne 0 ; then
        echo "Error in deploy.site target. Aborting..."
        exit 1
    fi
}

clean_features()
{
    echo "Cleaning features."
    ant -lib lib clean
    if test $? -ne 0 ; then
        echo "Cleaning features failed. Aborting..."
        exit 1
    fi
    return 0
}
#==============================
# main
#==============================
export LC_ALL=C
cd `dirname $0`

getopt $*
get_version

# [clean] case
if test "x$TARGET" = "xclean" ; then
    echo "Starting to clean features."
    cd sitetool
    clean_features
else
# [build] case
    echo "Starting to build features."
    find_eclipsehome
    find_antplugin
    check_plugins

    export ANT_HOME
    export PLUGINS_DIR
    export PATH=${PATH}:${ANT_HOME}/bin
    echo "ANT_HOME=${ANT_HOME}"
    echo "PLUGINS_DIR=${PLUGINS_DIR}"
    echo "PATH=${PATH}"

    cd sitetool
    build_features
fi
exit 0
# EOF
